package com.itheima.server.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.itheima.common.core.controller.BaseController;
import com.itheima.common.core.domain.AjaxResult;
import com.itheima.common.core.domain.entity.SysUser;
import com.itheima.common.core.page.TableDataInfo;
import com.itheima.server.domain.Clue;
import com.itheima.server.domain.ClueFollow;
import com.itheima.server.dto.*;
import com.itheima.server.service.ClueService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/clues")
@RequiredArgsConstructor
public class ClueController extends BaseController {
private final ClueService clueService;

/**
 * 获取线索列表
 * @param queryPage
 * @return
 */
@GetMapping("/clue/list")
public TableDataInfo list(ClueQueryPage queryPage){

    List<Clue> list = clueService.getList(queryPage);
    return getDataTable( list);


}

@PostMapping("/clue")
    public AjaxResult add(@RequestBody Clue clue){
    log.info("添加线索：{}",clue);
     clueService.add(clue);
    return AjaxResult.success();
}
/**
 * 获取线索详情
 * @param id
 * @return
 */
@GetMapping("/clue/{id}")
    public AjaxResult getCluById(@PathVariable Integer id){
   Clue clue = clueService.getCluById( id);
   return AjaxResult.success(clue);
}

    /**
     * 获取学科跟进列表
     * @param clueId
     * @return
     */
    @GetMapping("/record/list")
    public TableDataInfo  getSubjectList(Integer clueId){
     List<ClueFollow> list = clueService.getSubjectList(clueId);
    return getDataTable(list) ;
}

    /**
     *
     * 添加跟进
     * @return
     */
    @PostMapping("/record")
    public AjaxResult addRecord(@RequestBody ClueFollow clueFollow){
        clueService.addRecord(clueFollow);
        return AjaxResult.success();
    }
    /**
     * 线索作废
     * @param id
     * @param clueFalseReasonDto
     * @return
     */
    @PutMapping("/clue/false/{id}")
    public AjaxResult falseClue(@PathVariable Integer id,@RequestBody ClueFalseReasonDto  clueFalseReasonDto){
        clueService.falseClue(id, clueFalseReasonDto);
        return AjaxResult.success();
    }
    /**
     * 获取线索池列表
     * @param cluePoolPageDto
     * @return
     */
    @GetMapping("/clue/pool")
    public TableDataInfo getPoolList(CluePoolPageDto cluePoolPageDto){
        List<Clue> poolList = clueService.getPoolList(cluePoolPageDto);

        return getDataTable(poolList) ;
    }

    /**
     *
     * 捞取线索
     * @param clueGainDto
     * @return
     */
    @PutMapping("/clue/gain")
    public AjaxResult gainClue(@RequestBody ClueGainDto clueGainDto){
        List<Integer> ids = clueGainDto.getIds();
        clueService.gainClue(ids);
        return AjaxResult.success();
    }
    /**
     * 线索分配
     * @param cluAssignmentDto
     * @return
     */
    @PutMapping("clue/assignment")
    public AjaxResult assigment(@RequestBody CluAssignmentDto cluAssignmentDto){
        log.info("线索分配：{}",cluAssignmentDto);
        //根据userId查询用户保有量


       AjaxResult ajaxResult = clueService.assigment(cluAssignmentDto );
        return ajaxResult;
    }
    /**
     * 线索转商机
     * @param id
     * @param clue
     * @return
     */
    @PutMapping("clue/changeBusiness/{id}")
    public  AjaxResult changeBusiness(@RequestBody Clue clue,@PathVariable Integer id){

       AjaxResult ajaxResult =     clueService.changeBusiness(clue,id);

        return ajaxResult;


    }


    @PostMapping("/clue/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file){
        HashMap<String, Integer> map = new HashMap<>();
        map.put("successNum",null);
        map.put("failureNum",null);
        try {
            //读取所有数据到内存
            List<Clue> clueList = EasyExcel.read(file.getInputStream())
                    .head(Clue.class)
                    .sheet().doReadSync();
             clueService.saveList(clueList,map);
        } catch (IOException e) {
            log.error("导入出错 {}" ,e);
        }
        return success(map);
    }

}
